﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.RDLC
Partial Class rptTransPendSign
    Inherits System.Web.UI.Page

#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 25
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10

        LoadRptTransSign()
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
        LoadRptTransSign()
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
        LoadRptTransSign()
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
        LoadRptTransSign()
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
        LoadRptTransSign()
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
        LoadRptTransSign()
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
        LoadRptTransSign()
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
        LoadRptTransSign()
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        LoadRptTransSign()
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
            LoadRptTransSign()
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
        LoadRptTransSign()

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
            LoadRptTransSign()
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
        LoadRptTransSign()
    End Sub

#End Region

    Private Function LoadTransSign() As Data.DataTable
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        Dim strWhere As String = String.Empty

        Dim dtFromDate As DateTime
        Dim dtToDate As DateTime
        'to add in filtering for report - fromDate, toDate, Type(Created;Updated)
        If txtFromDate.Text <> "" And txtToDate.Text <> "" Then
            dtFromDate = Date.ParseExact(txtFromDate.Text, "dd-MMM-yyyy", Nothing)
            dtToDate = Date.ParseExact(txtToDate.Text, "dd-MMM-yyyy", Nothing)
            If dtFromDate > dtToDate Then
                lblMsgError.Text = "Date To cannot be earlier than Date From!"
                divError.Visible = True
                Return Nothing
                Exit Function
            Else
                objDB.AddParameter("FromDate", Data.SqlDbType.DateTime, dtFromDate)
                objDB.AddParameter("ToDate", Data.SqlDbType.DateTime, dtToDate.AddDays(1))
                objDB.OpenConnection()
                objDB.dbVariable.SQLString = "SELECT s.TransactDate, t.Initial, s.OpenFileRef, p.UnitNumber as 'PNO', " & _
                                        "ro.Road, pr.Project, lo.Location, st.State, 'SALE' as 'ListType', " & _
                                        "CASE s.TransListType WHEN '1' THEN 'SR' WHEN '2' THEN 'SC' WHEN '3' " & _
                                        "THEN 'SL' WHEN '4' THEN 'SI' ELSE '' END as 'Category', s.TransactedPrice " & _
                                        "FROM AW_SaleTransaction s LEFT JOIN AW_Staff t on s.TransactorId = t.StaffId " & _
                                        "LEFT JOIN AW_Property p on s.PropertyID = p.PropertyId " & _
                                        "LEFT JOIN aw_Road ro on ro.RoadId = p.RoadId " & _
                                        "LEFT JOIN aw_Location lo on lo.LocationId = ro.LocationId " & _
                                        "LEFT JOIN aw_State st on st.StateId = lo.StateId " & _
                                        "LEFT JOIN aw_Project pr on pr.ProjectId = p.ProjectId " & _
                                        "WHERE s.Status = '1' AND s.TransactDate BETWEEN @FromDate AND @ToDate UNION " & _
                                        "SELECT r.TransactDate, t.Initial, r.OpenFileRef, p.UnitNumber as 'PNO', " & _
                                        "ro.Road, pr.Project, lo.Location, st.State, 'RENT' as 'ListType', " & _
                                        "CASE r.TransListType WHEN '1' THEN 'RR' WHEN '2' THEN 'RC' WHEN '3' " & _
                                        "THEN 'RL' WHEN '4' THEN 'RI' ELSE '' END as 'Category', r.MonthlyRental as 'TransactedPrice' " & _
                                        "FROM AW_RentTransaction r LEFT JOIN AW_Staff t on r.TransactorId = t.StaffId " & _
                                        "LEFT JOIN AW_Property p on r.PropertyID = p.PropertyId " & _
                                        "LEFT JOIN aw_Road ro on ro.RoadId = p.RoadId " & _
                                        "LEFT JOIN aw_Location lo on lo.LocationId = ro.LocationId " & _
                                        "LEFT JOIN aw_State st on st.StateId = lo.StateId " & _
                                        "LEFT JOIN aw_Project pr on pr.ProjectId = p.ProjectId " & _
                                        "WHERE r.Status = '1' AND r.TransactDate BETWEEN @FromDate AND @ToDate " & lblHidSortType.Text
                objDB.FillData(TempDT, "AW_Transaction", Database.SQLCommandType.NormalString)
                objDB.CloseConnection()
                Return TempDT
            End If
        Else
            lblMsgError.Text = "Please select a date range!"
            divError.Visible = True
            Return Nothing
            Exit Function
        End If
    End Function

    Private Sub LoadRptTransSign()

        Dim Tempdt As New Data.DataTable

        Tempdt = LoadTransSign()

        If Tempdt IsNot Nothing Then
            'Creating a Paged Datasouce
            Dim objPaged As New PagedDataSource
            objPaged.DataSource = Tempdt.DefaultView
            objPaged.AllowPaging = True
            objPaged.PageSize = PageSize
            objPaged.CurrentPageIndex = CurrentPage

            lblHeadNoOfItem.Text = Tempdt.Rows.Count
            lblFootNoOfItem.Text = Tempdt.Rows.Count

            repTransaction.DataSource = objPaged
            repTransaction.DataBind()

            lblMaxPageCount.Text = objPaged.PageCount - 1
            lblTotalPage.Text = " of " & objPaged.PageCount
        End If
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        LoadRptTransSign()
    End Sub

    Protected Sub btnResetSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnResetSearch.Click
        ClearAllFields()
    End Sub

    Private Sub ClearAllFields()
        txtFromDate.Text = String.Empty
        txtToDate.Text = String.Empty
    End Sub

    Private Function SortType(ByVal strSortType As String) As String
        Dim strSort As String
        If Right(strSortType, 1) = "▼" Then
            strSort = "ASC"
        Else
            strSort = "DESC"
        End If
        Return strSort
    End Function

    Protected Sub lnkDateTrans_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkDateTrans.Click
        If lnkDateTrans.Text = "Transact Date▼" Then
            lnkDateTrans.Text = "Transact Date▲"
        Else
            lnkDateTrans.Text = "Transact Date▼"
        End If

        lblHidSortType.Text = " ORDER BY TransactDate " & SortType(lnkDateTrans.Text) & ", Initial " & SortType(lnkInitial.Text) & _
        ", OpenFileRef " & SortType(lnkRefNo.Text) & ", PNO " & SortType(lnkPNO.Text) & ", Road " & SortType(lnkRoad.Text) & _
        ", Project " & SortType(lnkProject.Text) & ", Location " & SortType(lnkLocation.Text) & ", State " & SortType(lnkState.Text) & _
        ", ListType " & SortType(lnkListType.Text) & ", Category " & SortType(lnkCategory.Text) & ", TransactedPrice " & SortType(lnkTransactedPrice.Text)
        LoadRptTransSign()
    End Sub

    Protected Sub lnkInitial_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkInitial.Click
        If lnkInitial.Text = "Transactor▼" Then
            lnkInitial.Text = "Transactor▲"
        Else
            lnkInitial.Text = "Transactor▼"
        End If

        lblHidSortType.Text = " ORDER BY Initial " & SortType(lnkInitial.Text) & ", TransactDate " & SortType(lnkDateTrans.Text) & _
        ", OpenFileRef " & SortType(lnkRefNo.Text) & ", PNO " & SortType(lnkPNO.Text) & ", Road " & SortType(lnkRoad.Text) & _
        ", Project " & SortType(lnkProject.Text) & ", Location " & SortType(lnkLocation.Text) & ", State " & SortType(lnkState.Text) & _
        ", ListType " & SortType(lnkListType.Text) & ", Category " & SortType(lnkCategory.Text) & ", TransactedPrice " & SortType(lnkTransactedPrice.Text)
        LoadRptTransSign()
    End Sub

    Protected Sub lnkRefNo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkRefNo.Click
        If lnkRefNo.Text = "Ref No▼" Then
            lnkRefNo.Text = "Ref No▲"
        Else
            lnkRefNo.Text = "Ref No▼"
        End If

        lblHidSortType.Text = " ORDER BY OpenFileRef " & SortType(lnkRefNo.Text) & ", TransactDate " & SortType(lnkDateTrans.Text) & _
        ", Initial " & SortType(lnkInitial.Text) & ", PNO " & SortType(lnkPNO.Text) & ", Road " & SortType(lnkRoad.Text) & _
        ", Project " & SortType(lnkProject.Text) & ", Location " & SortType(lnkLocation.Text) & ", State " & SortType(lnkState.Text) & _
        ", ListType " & SortType(lnkListType.Text) & ", Category " & SortType(lnkCategory.Text) & ", TransactedPrice " & SortType(lnkTransactedPrice.Text)
        LoadRptTransSign()
    End Sub

    Protected Sub lnkPNO_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPNO.Click
        If lnkPNO.Text = "PNO▼" Then
            lnkPNO.Text = "PNO▲"
        Else
            lnkPNO.Text = "PNO▼"
        End If

        lblHidSortType.Text = " ORDER BY PNO " & SortType(lnkPNO.Text) & ", TransactDate " & SortType(lnkDateTrans.Text) & _
        ", Initial " & SortType(lnkInitial.Text) & ", OpenFileRef " & SortType(lnkRefNo.Text) & ", Road " & SortType(lnkRoad.Text) & _
        ", Project " & SortType(lnkProject.Text) & ", Location " & SortType(lnkLocation.Text) & ", State " & SortType(lnkState.Text) & _
        ", ListType " & SortType(lnkListType.Text) & ", Category " & SortType(lnkCategory.Text) & ", TransactedPrice " & SortType(lnkTransactedPrice.Text)
        LoadRptTransSign()
    End Sub

    Protected Sub lnkRoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkRoad.Click
        If lnkRoad.Text = "Road Name▼" Then
            lnkRoad.Text = "Road Name▲"
        Else
            lnkRoad.Text = "Road Name▼"
        End If

        lblHidSortType.Text = " ORDER BY Road " & SortType(lnkRoad.Text) & ", TransactDate " & SortType(lnkDateTrans.Text) & _
        ", Initial " & SortType(lnkInitial.Text) & ", OpenFileRef " & SortType(lnkRefNo.Text) & ", PNO " & SortType(lnkPNO.Text) & _
        ", Project " & SortType(lnkProject.Text) & ", Location " & SortType(lnkLocation.Text) & ", State " & SortType(lnkState.Text) & _
        ", ListType " & SortType(lnkListType.Text) & ", Category " & SortType(lnkCategory.Text) & ", TransactedPrice " & SortType(lnkTransactedPrice.Text)
        LoadRptTransSign()
    End Sub

    Protected Sub lnkProject_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkProject.Click
        If lnkProject.Text = "Project Name▼" Then
            lnkProject.Text = "Project Name▲"
        Else
            lnkProject.Text = "Project Name▼"
        End If

        lblHidSortType.Text = " ORDER BY Project " & SortType(lnkProject.Text) & ", TransactDate " & SortType(lnkDateTrans.Text) & _
        ", Initial " & SortType(lnkInitial.Text) & ", OpenFileRef " & SortType(lnkRefNo.Text) & ", PNO " & SortType(lnkPNO.Text) & _
        ", Road " & SortType(lnkRoad.Text) & ", Location " & SortType(lnkLocation.Text) & ", State " & SortType(lnkState.Text) & _
        ", ListType " & SortType(lnkListType.Text) & ", Category " & SortType(lnkCategory.Text) & ", TransactedPrice " & SortType(lnkTransactedPrice.Text)
        LoadRptTransSign()
    End Sub

    Protected Sub lnkLocation_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLocation.Click
        If lnkLocation.Text = "Location▼" Then
            lnkLocation.Text = "Location▲"
        Else
            lnkLocation.Text = "Location▼"
        End If

        lblHidSortType.Text = " ORDER BY Location " & SortType(lnkLocation.Text) & ", TransactDate " & SortType(lnkDateTrans.Text) & _
        ", Initial " & SortType(lnkInitial.Text) & ", OpenFileRef " & SortType(lnkRefNo.Text) & ", PNO " & SortType(lnkPNO.Text) & _
        ", Road " & SortType(lnkRoad.Text) & ", Project " & SortType(lnkProject.Text) & ", State " & SortType(lnkState.Text) & _
        ", ListType " & SortType(lnkListType.Text) & ", Category " & SortType(lnkCategory.Text) & ", TransactedPrice " & SortType(lnkTransactedPrice.Text)
        LoadRptTransSign()
    End Sub

    Protected Sub lnkState_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkState.Click
        If lnkState.Text = "State▼" Then
            lnkState.Text = "State▲"
        Else
            lnkState.Text = "State▼"
        End If

        lblHidSortType.Text = " ORDER BY State " & SortType(lnkState.Text) & ", TransactDate " & SortType(lnkDateTrans.Text) & _
        ", Initial " & SortType(lnkInitial.Text) & ", OpenFileRef " & SortType(lnkRefNo.Text) & ", PNO " & SortType(lnkPNO.Text) & _
        ", Road " & SortType(lnkRoad.Text) & ", Project " & SortType(lnkProject.Text) & ", Location " & SortType(lnkLocation.Text) & _
        ", ListType " & SortType(lnkListType.Text) & ", Category " & SortType(lnkCategory.Text) & ", TransactedPrice " & SortType(lnkTransactedPrice.Text)
        LoadRptTransSign()
    End Sub

    Protected Sub lnkListType_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkListType.Click
        If lnkListType.Text = "Transaction Type▼" Then
            lnkListType.Text = "Transaction Type▲"
        Else
            lnkListType.Text = "Transaction Type▼"
        End If

        lblHidSortType.Text = " ORDER BY ListType " & SortType(lnkListType.Text) & ", TransactDate " & SortType(lnkDateTrans.Text) & _
        ", Initial " & SortType(lnkInitial.Text) & ", OpenFileRef " & SortType(lnkRefNo.Text) & ", PNO " & SortType(lnkPNO.Text) & _
        ", Road " & SortType(lnkRoad.Text) & ", Project " & SortType(lnkProject.Text) & ", Location " & SortType(lnkLocation.Text) & _
        ", State " & SortType(lnkState.Text) & ", Category " & SortType(lnkCategory.Text) & ", TransactedPrice " & SortType(lnkTransactedPrice.Text)
        LoadRptTransSign()
    End Sub

    Protected Sub lnkCategory_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCategory.Click
        If lnkCategory.Text = "Category▼" Then
            lnkCategory.Text = "Category▲"
        Else
            lnkCategory.Text = "Category▼"
        End If

        lblHidSortType.Text = " ORDER BY Category " & SortType(lnkCategory.Text) & ", TransactDate " & SortType(lnkDateTrans.Text) & _
        ", Initial " & SortType(lnkInitial.Text) & ", OpenFileRef " & SortType(lnkRefNo.Text) & ", PNO " & SortType(lnkPNO.Text) & _
        ", Road " & SortType(lnkRoad.Text) & ", Project " & SortType(lnkProject.Text) & ", Location " & SortType(lnkLocation.Text) & _
        ", State " & SortType(lnkState.Text) & ", ListType " & SortType(lnkListType.Text) & ", TransactedPrice " & SortType(lnkTransactedPrice.Text)
        LoadRptTransSign()
    End Sub

    Protected Sub lnkTransactedPrice_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkTransactedPrice.Click
        If lnkTransactedPrice.Text = "Transacted Price▼" Then
            lnkTransactedPrice.Text = "Transacted Price▲"
        Else
            lnkTransactedPrice.Text = "Transacted Price▼"
        End If

        lblHidSortType.Text = " ORDER BY TransactedPrice " & SortType(lnkTransactedPrice.Text) & ", TransactDate " & SortType(lnkDateTrans.Text) & _
        ", Initial " & SortType(lnkInitial.Text) & ", OpenFileRef " & SortType(lnkRefNo.Text) & ", PNO " & SortType(lnkPNO.Text) & _
        ", Road " & SortType(lnkRoad.Text) & ", Project " & SortType(lnkProject.Text) & ", Location " & SortType(lnkLocation.Text) & _
        ", State " & SortType(lnkState.Text) & ", ListType " & SortType(lnkListType.Text) & ", Category " & SortType(lnkCategory.Text)
        LoadRptTransSign()
    End Sub

    Protected Sub imgGenerate_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgGenerate.Click
        Dim TempDT As New Data.DataTable

        TempDT = LoadTransSign()

        If TempDT IsNot Nothing Then
            Dim objRDLC As New RDLC("report/rptPendingSign.rdlc", "TransPendSign")
            TempDT.TableName = "dsTransPendSign_AW_Transaction"
            objRDLC.AddDatabase(TempDT)
            objRDLC.ExportReport(RDLC.ExportTypeFormat.PDF, HttpContext.Current)
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("StaffId") Is Nothing Then
            Response.Redirect("Default.aspx")
        Else
            lblStaffUID.Text = Session("StaffId").ToString
        End If
    End Sub
End Class
